package org.hot100_2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class leetcode438 {
    public List<Integer> findAnagrams(String s ,String p){

        int sLength = s.length();
        int pLength = p.length();

        List<Integer> res = new ArrayList<>();

        if (pLength>sLength){
            return res;
        }

        char [] sCount = new char[26];
        char [] pCount = new char[26];

        for (int i = 0; i < pLength; i++) {
            sCount[s.charAt(i)-'a']++;
            pCount[p.charAt(i)-'a']++;
        }

        if (Arrays.equals(sCount,pCount)){
            res.add(0);
        }

        for (int i = 0; i < sLength-pLength; i++) {

            sCount[s.charAt(i)-'a']--;
            sCount[s.charAt(i+pLength)-'a']++;

            if (Arrays.equals(sCount,pCount)){
                res.add(i+1);
            }
        }


        return res;
    }
}
